package com.kgc.warehouse.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kgc.warehouse.system.common.CodeMsg;
import com.kgc.warehouse.system.common.ResponseResult;
import com.kgc.warehouse.system.dto.DeptPageDto;
import com.kgc.warehouse.system.entity.Dept;
import com.kgc.warehouse.system.service.DeptService;
import com.kgc.warehouse.system.vo.DeptVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("dept")
@Slf4j
public class DeptController {

    @Autowired
    private DeptService deptService;
    @RequestMapping("treeSelect")
    public ResponseResult deptSelect()
    {
        List<Dept> list = deptService.list();
        return new ResponseResult(list,null, CodeMsg.SUCCESS);
    }

    @RequestMapping("loadAllDept")
    @RequiresPermissions("dept:view")
    public ResponseResult loadAllDept(DeptPageDto deptPageDto)
    {
        return deptService.selectPage(deptPageDto);
    }

    @RequestMapping("add")
    @RequiresPermissions("dept:create")
    public  ResponseResult add(@RequestBody DeptVo deptVo)
    {
        log.info(String.valueOf(deptVo));
        return deptService.add(deptVo);
    }

    @RequestMapping("delete")
    @RequiresPermissions("dept:delete")
    public ResponseResult delete(Integer id)
    {
        return deptService.deleteById(id);
    }

    @RequestMapping("getById")
    public ResponseResult getById(Integer id, HttpSession session)
    {
        Dept byId = deptService.getById(id);
        session.setAttribute("deptById",byId);
        return new ResponseResult(byId,null,CodeMsg.SUCCESS);
    }

    @RequestMapping("update")
    @RequiresPermissions("dept:update")
    public ResponseResult update(Dept dept,HttpSession session)
    {

        return deptService.updateDeptById(dept);
    }

}
